package myjava.i_collection;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: cty
 * @Date: 2019/11/15 上午10:00
 * @Description: 测试Map接口，实现类如下：
 * HashMap——效率高，线程不安全，K和V可为空
 * HashTable——线程安全，效率低，K和V不可为空
 * TreeMap——可以排序
 * @version: 1.0
 */
public class G_TestHashMap {
    public static void main(String[] args) {
        /* Map基本方法测试 */
        Map<Integer,String> m1 = new HashMap<>();

        m1.put(1,"one");
        m1.put(2,"two");
        m1.put(3,"three");
        System.out.println(m1.get(1));
        System.out.println(m1.size());
        System.out.println(m1.containsKey(2));
        System.out.println(m1.containsKey(4));
        System.out.println(m1.containsValue("three"));
        System.out.println(m1.containsValue("four"));

        Map<Integer,String> m2 = new HashMap<>();
        m2.put(4,"四");
        m2.put(5,"五");
        System.out.println(m2);

        m1.putAll(m2);
        System.out.println(m1);

        //map中键不能重复！若重复，新值覆盖旧值
        m1.put(3,"三");
        System.out.println(m1);

        System.out.println("##########################################################");
        /* map测试2 */
        Employee e1 = new Employee(1001,"cty",50000);
        Employee e2 = new Employee(1002,"小天",20000);
        Employee e3 = new Employee(1003,"小宇",30000);

        Map<Integer,Employee> map = new HashMap<>();

        map.put(1001,e1);
        map.put(1002,e2);
        map.put(1003,e3);

        Employee emp = map.get(1001);
        System.out.println(emp.getName());
    }
}

class Employee{
    private int id;
    private String name;
    private double salary;

    public Employee(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}
